using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace k.Frame.Utility
{
    public abstract class Validators
    {
        /// <summary>
        /// Validates an e-mail address against a regular expression pattern
        /// </summary>
        /// <param name="eMail">E-Mail address to validate</param>
        /// <returns>true if e-mail address is valid. false otherwise</returns>
        public static bool isValidEMail(string eMail)
        {
            return Regex.IsMatch(eMail, "^[A-Za-z0-9\\._]+[@]{1}[A-Za-z0-9_]{2,}[\\.]{1}[A-Za-z0-9\\._]+$");
        }
        
        public static bool TaxNumberCheck(string taxNumber)
        {
            if (Regex.IsMatch(taxNumber, "^\\d{10}$"))
            {
                int baseValue = 512;
                int total = 0;
                char[] first9Chars = taxNumber.Substring(0, 9).ToCharArray();
                int[] first9CharsCalculated = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                string lastChar = taxNumber.Substring(9, 1);

                for (int i = 1; i <= 9; i++)
                {
                    first9CharsCalculated[i - 1] = k.Frame.Utility.MathExtensions.Mod(Convert.ToInt32(first9Chars[i - 1].ToString()) + (10 - i), 10);
                    int temp = (baseValue * first9CharsCalculated[i - 1]);
                    temp = k.Frame.Utility.MathExtensions.Mod(temp, 9);
                    if (first9CharsCalculated[i - 1] != 0 && temp == 0)
                    {
                        temp = 9;
                    }
                    total += temp;
                    baseValue /= 2;
                }

                int lastDigit = 0;
                if (k.Frame.Utility.MathExtensions.Mod(total, 10) == 0)
                {
                    lastDigit = 0;
                }
                else
                {
                    // strLastDigit = ((strTotal \ 10 + 1) * 10) - strTotal
                    lastDigit = ((total / 10) + 1) * 10 - total;
                }

                if (lastDigit.ToString() == lastChar)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
